import CryptoJS from 'crypto-js/crypto-js';

export function encrypt(msg, key, iv) {
  return CryptoJS.AES.encrypt(msg, key, {
    iv,
    padding: CryptoJS.pad.Pkcs7,
    mode: CryptoJS.mode.CBC,
  });
}

export function decrypt(cipherText, key, iv) {
  return CryptoJS.AES.decrypt({ ciphertext: cipherText }, key, {
    iv,
    padding: CryptoJS.pad.Pkcs7,
    mode: CryptoJS.mode.CBC,
  });
}

export function encryptAuto(str) {
  const key = CryptoJS.enc.Base64.parse('aW5wdXJrc2hzaA==');
  const iv = CryptoJS.enc.Base64.parse('aW5wdXJrc2hzaA==');
  const encrypted = encrypt(str, key, iv);
  const encryptStr = encrypted.ciphertext.toString();

  return encryptStr;
}

export function decryptAuto(str) {
  const key = CryptoJS.enc.Base64.parse('ZGIyMTM5NTYxYzlmZTA2OA==');
  const iv = CryptoJS.enc.Base64.parse('ZGIyMTM5NTYxYzlmZTA2OA==');
  const decrypted = decrypt(str, key, iv);
  const decryptStr = decrypted.words.toString();

  return decryptStr;
}
